/*
 * Copyright 2017 Baiyyy
 * @(#)DeviceServiceImpl.java
 */

package com.byyy.ccts.modules.cc.service.impl;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.byyy.ccts.core.common.service.impl.CommonServiceImpl;
import com.byyy.ccts.core.query.data.Page;
import com.byyy.ccts.core.query.data.PageImpl;
import com.byyy.ccts.core.query.data.Pageable;
import com.byyy.ccts.core.query.data.Queryable;
import com.byyy.ccts.core.query.parse.QueryToWrapper;
import com.byyy.ccts.modules.cc.Global;
import com.byyy.ccts.modules.cc.entity.ccts.TADevice;
import com.byyy.ccts.modules.cc.entity.ccts.TSDeviceOptCmd;
import com.byyy.ccts.modules.cc.mapper.DeviceOptCmdMapper;
import com.byyy.ccts.modules.cc.service.IDeviceOptCmdService;
import com.byyy.ccts.modules.cc.service.IDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 设备操作指令
 * Created by DELL on 2018-5-14.
 */
@Transactional
@Service("deviceOptCmdService")
public class DeviceOptCmdServiceImpl extends CommonServiceImpl<DeviceOptCmdMapper, TSDeviceOptCmd> implements IDeviceOptCmdService {

    /** 设备服务类 */
    @Autowired
    private IDeviceService deviceService;

    /** 设备操作指令 */
    @Autowired
    private DeviceOptCmdMapper deviceOptCmdMapper;

    /**
     * 修改设备命令
     * @param entity
     */
    @Override
    public void changeDeviceParam(TADevice entity) {
        TSDeviceOptCmd cmd = null;
        // 根据ID获取设备信息
        TADevice device = this.deviceService.selectById(entity.getId());
        if (device == null) {
            return;
        }
        // 判断设备参数-间隔时间是否有修改
        // 20200922 因为设备在运行时，会自动修改设备默认时间间隔，故此处不再需要针对于设备时间间隔进行单独设置指令
//        if (!device.getMinuteDefault().equals(entity.getMinuteDefault())) {
//            this.setDeviceTimeInterval(device, entity.getMinuteDefault());
//        }
        // 判断设备参数-预警温度值是否有修改
        if ((!device.getHighT().equals(entity.getHighT()))
                || (!device.getLowT().equals(entity.getLowT()))) {
            // 判断设备版本号,如果不是V1.2的话,在修改预警温度值的时候,需要先开启设备预警功能
            if (!Global.VER_1_2.equals(entity.getVer())) {
                cmd = new TSDeviceOptCmd();
                cmd.setDeviceId(device.getId());
                cmd.setDeviceSn(device.getSn());
                cmd.setCmd("73");
                cmd.setRemarks("温度湿度光感预警报警总开关");
                this.insert(cmd);
            }

            String uuid = cmd.getId();
            cmd = new TSDeviceOptCmd();
            cmd.setDeviceId(device.getId());
            cmd.setDeviceSn(device.getSn());
            cmd.setCmd("64");
            cmd.setParam(entity.getHighT() + "," + entity.getLowT());
            cmd.setRemarks("设定温度报警");
            if (!Global.VER_1_2.equals(entity.getVer())) {
                cmd.setPreId(uuid);
                cmd.setPreCmd("73");
            }
            this.insert(cmd);

        }

    }

    /**
     * 获取设备的指令
     * @param deviceSn
     * @param cmd
     * @param state
     * @return
     */
    @Override
    public List<TSDeviceOptCmd> getDeviceOptCmd(String deviceSn, String cmd, String state) {
        return this.deviceOptCmdMapper.getDeviceOptCmd(deviceSn, cmd, state);
    }

    @Override
    public Page<TSDeviceOptCmd> deviceOptCmdList(Queryable queryable, Wrapper<TSDeviceOptCmd> wrapper) {
        QueryToWrapper<TSDeviceOptCmd> queryToWrapper = new QueryToWrapper<>();
        queryToWrapper.parseCondition(wrapper, queryable);
        // 排序问题
        queryToWrapper.parseSort(wrapper, queryable);
        Pageable pageable = queryable.getPageable();
        com.baomidou.mybatisplus.plugins.Page<TSDeviceOptCmd> page = new com.baomidou.mybatisplus.plugins.Page<TSDeviceOptCmd>(
                pageable.getPageNumber(), pageable.getPageSize());
        com.baomidou.mybatisplus.plugins.Page<TSDeviceOptCmd> content = deviceOptCmdListPage(page, wrapper);
        return new PageImpl<TSDeviceOptCmd>(content.getRecords(), queryable.getPageable(), content.getTotal());
    }

    @Override
    public com.baomidou.mybatisplus.plugins.Page<TSDeviceOptCmd> deviceOptCmdListPage(com.baomidou.mybatisplus.plugins.Page<TSDeviceOptCmd> page, Wrapper<TSDeviceOptCmd> wrapper) {
        page.setRecords(baseMapper.deviceOptCmdListPage(page, wrapper));
        return page;
    }

    /**
     * 设置设备上传时间间隔
     * @param device
     */
    @Override
    public void setDeviceTimeInterval (TADevice device, String interval) {
        TSDeviceOptCmd cmd = new TSDeviceOptCmd();
        cmd.setDeviceId(device.getId());
        cmd.setDeviceSn(device.getSn());
        cmd.setCmd("62");
        cmd.setParam(interval);
        cmd.setRemarks("设置通用报位间隔");
        this.insert(cmd);
        cmd.setId(null);
        cmd.setCmd("78");
        cmd.setRemarks("设置报警报位间隔");
        this.insert(cmd);
    }

}
